package com.util;

import com.dao.UserDao;
import com.entity.Message;
import com.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;
import java.util.List;

//管理员工具类
public class UserUtil {

    public static Message isExist(User user) throws IOException {

        Message message = new Message();
        String config = "SqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(config);
//        InputStreamReader reader = new InputStreamReader(in);
//
//        System.out.println(reader);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        SqlSession session = factory.openSession();

        UserDao userDao = session.getMapper(UserDao.class);

        List<User> users = userDao.findUser(user);

        session.commit();
        if(users.size() == 0){
            message.setFlag(false);
        }else{
            message.setFlag(true);
        }

        if(session!=null){
            session.close();
        }
        if (in!=null){
            in.close();
        }
        return message;

    }
}
